﻿#include "stdafx.h"
#include "Logic.h"
#include "MobileGameDlg.h"
#include "resource.h"
//写任务进度
void WriteTaskSpeed(INT iDmIndex, CString strTask, INT roleIndex)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format(L"%Y-%m-%d");
	CString str;
	str.Format(L"%s_%s_%d", strTime, strTask, roleIndex);
	CString strAccount = m_ScriptSet[iDmIndex].m_GameInfoSt.m_strAccount;
	g_TaskSpeedFile.SetInt(strAccount, str, 1);
}

//读任务进度
BOOL ReadTaskSpeed(INT iDmIndex, CString strTask, INT roleIndex)
{
	CTime time = CTime::GetCurrentTime();
	CString strTime = time.Format(L"%Y-%m-%d");
	CString str;
	str.Format(L"%s_%s_%d", strTime, strTask, roleIndex);
	DbgOut(L"[sdn] 任务进度-%s", str);
	CString strAccount = m_ScriptSet[iDmIndex].m_GameInfoSt.m_strAccount;
	return g_TaskSpeedFile.GetInt(strAccount, str);
}

//获取日期和时间
void GetDateAndTime(int& iCurWeekDay, int& iCurHour)
{
	CTime t = CTime::GetCurrentTime(); //获取系统日期
									   //int d = t.GetDay(); //获得几号
									   //int y = t.GetYear(); //获取年份
									   //int m = t.GetMonth(); //获取当前月份
	iCurHour = t.GetHour(); //获取当前为几时
							//int mm = t.GetMinute(); //获取分钟
							//int s = t.GetSecond(); //获取秒
	iCurWeekDay = t.GetDayOfWeek(); //获取星期几，注意1为星期天，7为星期六
}


void MoveClick(INT iDmIndex, LONG x, LONG y, BOOL bFlag)
{
	int iRet = RandomNumber(2, 6);
	m_DM[iDmIndex]->MoveTo(x + iRet, y + iRet);
	Sleep(200);
	if (bFlag)
		m_DM[iDmIndex]->LeftDoubleClick();
	else
		m_DM[iDmIndex]->LeftClick();
	Sleep(300);
}

void KeyPressEX(INT iDmIndex, CString ch)
{
	m_DM[iDmIndex]->LockInput(1);
	m_DM[iDmIndex]->KeyPressChar(ch);
	m_DM[iDmIndex]->LockInput(0);
}

//拖动不释放
void Drag_NoUp(int iDmIndex, INT x1, INT y1, INT x2, INT y2, INT iTime)
{

	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(50);
	m_DM[iDmIndex]->LeftDown();
	Sleep(50);
	m_DM[iDmIndex]->MoveTo(x2, y2);
	Sleep(iTime);
}

void Drag(int iDmIndex, INT x1, INT y1, INT x2, INT y2, INT iTime)
{
	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(50);
	m_DM[iDmIndex]->LeftDown();
	Sleep(50);
	m_DM[iDmIndex]->MoveTo(x2, y2);
	Sleep(iTime);
	m_DM[iDmIndex]->LeftUp();
}

//移动拖拽
void ClickAndDrag(int iDmIndex, INT x1, INT y1, INT x2, INT y2)
{

	m_DM[iDmIndex]->LockInput(1);
	m_DM[iDmIndex]->MoveTo(x1, y1);
	Sleep(100);
	m_DM[iDmIndex]->LeftDown();
	Sleep(100);

	if (x1 < x2)
	{
		while (true)
		{
			x1 = x1 + 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (x1 == x2)
				break;
			else
			{
			}
		}
	}
	if (x1 > x2)
	{
		while (true)
		{
			x1 = x1 - 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (x1 == x2)
				break;
			else
			{
			}
		}
	}
	if (y1 < y2)
	{
		while (true)
		{
			y1 = y1 + 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (y1 == y2)
				break;
			else
			{
			}
		}
	}
	if (y1 > y2)
	{
		while (true)
		{
			y1 = y1 - 1;
			m_DM[iDmIndex]->MoveTo(x1, y1);
			Sleep(10);
			if (y1 == y2)
				break;
		}
	}

	m_DM[iDmIndex]->LeftUp();
	Sleep(100);
	m_DM[iDmIndex]->LockInput(0);
}

//显示控制台每个账号的各种信息
void ShowConsoleInfo(int iDmIndex, int iSubIndex)
{
	switch (iSubIndex)
	{
	case 1://句柄
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(m_ScriptSet[iDmIndex].m_hGameHandle));

	}break;
	case 4:
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strServer);
	}break;
	case 5://序号
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex));

	}break;
	case 6://银币
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strMoney);
	}break;
	case 7://等级
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strLv);

	}break;
	case 8://模拟器序号
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, NumberToString(iDmIndex));

	}break;
	case 9://任务状态
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState);

	}break;
	case 10://运行状态
	{
		g_MobileGameDlg->m_list_info.SetItemText(m_ScriptSet[iDmIndex].m_iControlIndex, iSubIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_strRunState);

	}
	default:
		break;
	}
}


//获取父窗口的子窗口句柄
LONG GetSubWindow(LONG father)
{
	CString strClass, strTitle;
	LONG pWnd, pWnd1 = NULL;
	TCHAR buffer[256];
	TCHAR buffer1[256];
	CRect rect;
	pWnd = (LONG) ::GetWindow((HWND)father, GW_CHILD);     // 查找对话框中的子控件

	while (pWnd)
	{
		::GetClassName((HWND)pWnd, buffer, 256);
		strClass = buffer;
		::GetWindowText((HWND)pWnd, buffer1, 256);
		strTitle = buffer1;
		::GetWindowRect((HWND)pWnd, &rect);

		if (strClass == "RenderWindow"&& strTitle == "TheRender" && rect.Width() == 800 && rect.Height() == 600)//"RenderWindowWindow")
			return (LONG)pWnd;

		pWnd1 = GetSubWindow(pWnd);       // 查找下一个子控件
		if (pWnd1)
			return pWnd1;
		else
			pWnd = (LONG) ::GetNextWindow((HWND)pWnd, 2);
	}
	return pWnd;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//处理当前任务的流程
int DealCurTaskProcess(int nIndex)
{
	m_GState = true; //任务流程标记
	 m_GGameHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
	int ret = m_GlobalDM->BindWindowEx(m_GGameHwnd, L"gdi", L"normal", L"normal", L"", 0);

	//CString str = m_GlobalDM->FindPicE(2, 423, 797, 466, L"taskCao.bmp", L"303030", 0.90, 0);
	//CString str1 =m_GlobalDM->FindPicEx(2, 423, 797, 466, L"taskCao.bmp", L"303030", 0.90, 0);
	//CString str2= m_GlobalDM->FindPicExS(2, 423, 797, 466, L"taskCao.bmp", L"303030", 0.90, 0);


	//while (true)
	//{
	//	time_t timep;
	//	time(&timep);
	//	CString sTimeBmp;
	//	sTimeBmp.Format(L"%d.bmp", timep);
	//	m_GlobalDM->Capture(0, 0, 800, 600, sTimeBmp);
	//	m_GlobalDM->delay(2000);
	//}
	//return 0;
	//if (!StartLoginClient(nIndex)) {
	//	return 0;
	//}
	//if (!StartLoginDebug(nIndex)) {
	//	return 0;
	//}
	if (!MainTask(nIndex)) {
		return 0;
	}

	int iLoginRet = -1;
	int iRet = -1; 
	//if (g_IniFile.GetInt(_T("Set"), _T("调试模式")) == 1)
	//{
	//	if (!StartLoginDebug(iDmIndex))
	//	{
	//		return -1;
	//	}
	//}
	//else
	//{
	//	//
	//}

	//Sleep(10000);//等待到游戏里面

	//DbgOut(L"[sdn] iDmIndex:%d--RoleIndex:%d", iDmIndex, m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex);


	//CStringArray strCurTaskArray;
	//g_IniFile.GetSectionKeys(L"CurTask", strCurTaskArray);
	////先判断是一键起号 还是先组队副本后普通日常
	//iRet = g_IniFile.GetInt(L"Set", L"执行");
	//if (iRet == 1)//起号
	//{
	//	for (int i = 0; i < strCurTaskArray.GetSize(); i++)
	//	{
	//		CString strTaskName = g_IniFile.GetString(L"CurTask", NumberToString(i));
	//		if (strTaskName == L"主线任务")
	//		{
	//			MainTask(iDmIndex);
	//		}
	//	}
	//}
	//else if (iRet == 2)//日常
	//{
	//	for (int i = 0; i < strCurTaskArray.GetSize(); i++)
	//	{
	//		CString strTaskName = g_IniFile.GetString(L"CurTask", NumberToString(i));
	//		if (strTaskName == L"万事通的考验")
	//		{
	//			WanShiTongDeKaoYan(iDmIndex);
	//		}
	//		if (strTaskName == L"学院任务")
	//		{
	//			DbgOut(L"[sdn] 学院任务");
	//			//XueYuanRenWu(iDmIndex);
	//		}

	//	}
	//}

	return 0;
	//DbgOut("[sdn] 从配置文件找要做的任务列表 结束");
}

bool StartLoginClient(int nIndex) {
	int nCount = 0;
	while (m_GState)
	{
		VARIANT _x, _y;
		long nMapHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
		if (nMapHwnd > 0) {
			KillProcess(L"MapleStory.exe");
		}
		nMapHwnd = (long)m_GlobalDM->FindWindow(L"", L"MapleStory");
		if (nMapHwnd > 0) {
			KillProcess(L"GameLauncher.exe");
		}

		m_GlobalDM->delay(1000);
		CString sPath;
		Part:
		g_pPage1->GetDlgItemTextW(IDC_EDIT_Simulator_Path, sPath);
		m_GlobalDM->FindPic(0, 0, 1366, 768, L"game.bmp", L"202020", 0.9, 0, &_x, &_y);
		m_GlobalDM->MoveTo(_x.lVal+15, _y.lVal+5);
		m_GlobalDM->delay(1000);
		m_GlobalDM->LeftDoubleClick();
		m_GlobalDM->delay(1000);

		nMapHwnd = (long)m_GlobalDM->FindWindow(L"", L"MapleStory");
		if (nMapHwnd <= 0) {
			goto Part;
		}
		m_GlobalDM->SetWindowState((long)nMapHwnd, 12);
		m_GlobalDM->delay(1000);
		//m_GlobalDM->MoveWindow((long)nMapHwnd, -10, 0);
		m_GlobalDM->delay(1000);
		int ret = m_GlobalDM->BindWindowEx((long)nMapHwnd, L"normal", L"windows3", L"windows", L"", 0);
		FindPic(0, 0, 1366, 768, L"loginBtn.bmp", &_x, &_y);
		if (_x.lVal > 0) {
			m_GlobalDM->delay(1000);
			m_GlobalDM->MoveTo(_x.lVal+20, _y.lVal+5);
			m_GlobalDM->delay(1000);
			m_GlobalDM->LeftClick();
		}
		m_GlobalDM->UnBindWindow();
		m_GlobalDM->delay(1000);
		for (int i = 0; i < 120; i++)
		{
			//m_GlobalDM->IsBind()
			//查找登录成功标志
			m_GGameHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
			if (m_GGameHwnd > 0 && !m_GlobalDM->IsBind(m_GGameHwnd)) {
				ret = m_GlobalDM->BindWindowEx(m_GGameHwnd, L"gdi", L"normal", L"normal", L"", 0);
				if (ret != 1) {
					m_GNum--;//全局索引号-1重新操作
					return false;
				}
			}
			m_GlobalDM->FindPic(0, 0, 800, 600, L"gameLogo.bmp|code.bmp|password.bmp", L"202020", 0.9, 0, &_x, &_y);
			if (_x.lVal > 0) {
				return true;
			}
			m_GlobalDM->delay(2000);
		}
		nCount++;
		if (nCount > 10) {//启动客户端失败
			m_GNum--;//全局索引号-1重新操作
			return false;
		}
	}

}

bool StartLoginDebug(int nIndex)
{
	VARIANT _x, _y;
	
	int nCount = 0;
	long nMapHwnd = (long)m_GlobalDM->FindWindow(L"MapleStoryClass", L"MapleStory");
	//int ret = m_GlobalDM->BindWindowEx((long)nMapHwnd, L"gdi", L"normal", L"windows", L"", 0);

	bool flag=false;
	m_GlobalDM->MoveWindow(nMapHwnd,0, 0);
	//m_GlobalDM
	while (m_GState)
	{
		m_GlobalDM->MoveToEx(100,100, 30, 45);
		m_GlobalDM->FindPic(294, 278, 437, 311, L"code.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"输入账号");
			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
			m_GlobalDM->delay(1000);
			m_GlobalDM->LeftClick();
			m_GlobalDM->delay(1000);
			//m_GlobalDM->KeyPressStr(g_MobileGameDlg->m_list_info.GetItemText(nIndex, 1), 100);
			m_GlobalDM->SendString2(nMapHwnd, g_MobileGameDlg->m_list_info.GetItemText(nIndex, 1));
			m_GlobalDM->delay(1000);
		}
		m_GlobalDM->delay(2000);
		m_GlobalDM->MoveToEx(100, 100, 30, 45);
		m_GlobalDM->delay(500);
		m_GlobalDM->FindPic(283, 308, 447, 332, L"password.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"输入密码");
			m_GlobalDM->MoveTo(_x.lVal+50, _y.lVal+8);
			Sleep(1000);
			m_GlobalDM->LeftClick();
			Sleep(1000);
			m_GlobalDM->SendString2(nMapHwnd, g_MobileGameDlg->m_list_info.GetItemText(nIndex, 2));
			Sleep(2000);
		}

		m_GlobalDM->FindPic(453 - 5, 298 - 5, 487+5, 317+5, L"login.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if(_x.lVal > 0) {
				OutPutListView(nIndex, L"点击登录");
				Sleep(1000);
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
				m_GlobalDM->LeftClick();
		}

		m_GlobalDM->FindPic(330 - 5, 282 - 5, 359+5, 302+5, L"checkCode.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"选择账号");
			m_GlobalDM->MoveTo(_x.lVal - RandomNumber(10, 30), _y.lVal + 46);
			m_GlobalDM->LeftDoubleClick();
			m_GlobalDM->delay(2000);
		}

		Sleep(2000);
		m_GlobalDM->FindPic(98 - 5, 0, 147+5, 23+5, L"loginEnter.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"回车选择");
			m_GlobalDM->KeyPress(13);
			m_GlobalDM->delay(2000);
		}
		m_GlobalDM->FindPic(271 - 5, 190 - 5, 336 + 5, 214+5, L"loginEnter1.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"回车选择");
			m_GlobalDM->KeyPress(13);
			m_GlobalDM->delay(2000);
		}
		m_GlobalDM->FindPic(625 - 5, 401 - 5, 719 + 5, 430 + 5, L"twoCodeWnd.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"回车选择");
			m_GlobalDM->KeyPress(13);
	
			m_GlobalDM->delay(2000);
		}
		m_GlobalDM->FindPic(430 - 5, 420 - 5, 472 + 5, 436 + 5, L"twoCodeKey.bmp", L"202020", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0 && !flag) {
			CString code = g_MobileGameDlg->m_list_info.GetItemText(nIndex, 3);
			CString code1;
			for (int i = 0; i < code.GetLength(); i++)
			{
				
				m_GlobalDM->delay(500);
				code1 = code1 + code.Mid(i, 1);
				CString strbmp = code.Mid(i, 1) + L".bmp";
				CString sTwoCode = L"输入二密" + code.Mid(i, 1);
				OutPutListView(nIndex, sTwoCode);
				m_GlobalDM->FindPic(126, 222, 675, 417, strbmp, L"202020", 0.90, 0, &_x, &_y);
				//
				if (_x.lVal > 0) {
					m_GlobalDM->MoveToEx(_x.lVal + 10, _y.lVal + 5, 10, 10);
					m_GlobalDM->LeftClick();
					m_GlobalDM->delay(1500);
				}
			}
			for (int i = 0; i < 5; i++)
			{
				m_GlobalDM->FindPic(298, 415, 399, 441, L"enterCode.bmp", L"303030", m_fSim, 0, &_x, &_y);
				if (_x.lVal > 0) {
					OutPutListView(nIndex, L"二密确认");
					m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 5);
					m_GlobalDM->LeftClick();
					m_GlobalDM->delay(1500);
					flag=true;
				}
			}
		}
		//
		m_GlobalDM->FindPic(281, 213, 524, 350, L"twoCodeError.bmp", L"303030", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			//TODO: 二密错误 处理 
		}



		if (flag) {
			m_GlobalDM->delay(1500);
			m_GlobalDM->FindPic(685, 538, 799, 588, L"loginSuccess.bmp|loginSuccess1.bmp", L"303030", m_fSim, 0, &_x, &_y);
			{
				if (_x.lVal > 0) {
					break;
				}
			}
		}
		nCount++;
		if (nCount > 30) {//启动客户端失败
			m_GNum--;//全局索引号-1重新操作
			return false;
		}
		m_GlobalDM->delay(1500);
	}
	
	//TODO:循环操作登录 达到一定登录次数未正常登录 改变状态返回

	//TODO:查找客户端是否存在
	//TODO:不存在则启动客户端存在则强制关闭客户端
	//TODO:输入账号密码
	//TODO:选择专属账号
	//TODO:输入二密
	//TODO:进入游戏查找是否进入成功


	return true;
}

void ZhiXian(int iDmIndex)
{
}

int MainTask(int nIndex)
{
	VARIANT _x, _y;
	bool flag = false;

	while (true)
	{
		m_GlobalDM->MoveToEx(100, 100, 50, 50);

		m_GlobalDM->FindPic(732, 56, 779, 88, L"closeWnd2.bmp", L"303030", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			m_GlobalDM->delay(1000);
			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal+10, 20, 15);
			m_GlobalDM->delay(1000);
			m_GlobalDM->LeftClick();
			m_GlobalDM->delay(1000);
		}
		m_GlobalDM->FindPic(601, 238, 632, 266, L"closeWnd3.bmp", L"303030", m_fSim, 0, &_x, &_y);
		if (_x.lVal > 0) {
			m_GlobalDM->delay(1000);
			m_GlobalDM->MoveToEx(_x.lVal+10, _y.lVal + 10, 20, 15);
			m_GlobalDM->delay(1000);
			m_GlobalDM->LeftClick();
			m_GlobalDM->delay(1000);
		}

		m_GlobalDM->FindPic(420, 310, 523, 353, L"okWnd.bmp|okWnd1.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 10, 10);
				m_GlobalDM->delay(1000);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1000);
			}
			m_GlobalDM->FindPic(0, 155, 153, 217, L"taskRun.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1500);
			}
			m_GlobalDM->FindPic(224, 196, 662, 400, L"taskDong.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1500);
			}
			m_GlobalDM->FindPic(226 - 5, 286 - 5, 561 + 5, 478 + 5, L"taskBoos.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1500);
			}
			m_GlobalDM->MoveToEx(100, 100, 50, 50);
			m_GlobalDM->FindPic(313, 493, 491, 527, L"taskBoosOk.bmp", L"303030", m_fSim, 0, &_x, &_y);
			if (_x.lVal > 0) {
				m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(1500);
			}
		m_GlobalDM->delay(2500);
		FindPic(170, 358, 539, 456, L"taskPlan.bmp", &_x, &_y);
		
		//FindPic(443, 245, 795, 317, L"tagPlan.bmp", &_x, &_y);//飞机定位
		if (_x.lVal > 0) {
			flag = true;
			for (int i = 0; i < 10; i++)
			{
				FindPic(3, 376, 776, 462, L"taskNpc3.bmp", &_x, &_y);
				if (_x.lVal > 0) {
					m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal+20);
					m_GlobalDM->LeftClick();
					m_GlobalDM->delay(2300);
					break;
				}
				
			}
			for (size_t i = 0; i < 5; i++)
			{
				FindPic(65, 499, 364, 535, L"choseTeamTask.bmp", &_x, &_y);
				if (_x.lVal > 0) {
					m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal + 5);
					m_GlobalDM->LeftClick();
					m_GlobalDM->delay(1300);
				}
				FindPic(468, 566, 567, 597, L"choseTeamTaskNext.bmp", &_x, &_y);
				if (_x.lVal > 0) {
					m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal);
					m_GlobalDM->LeftClick();
					m_GlobalDM->delay(1300);
					//TODO:查找任务是否已完成 完成标记m_GState为假  taskBossEnd.bmp
					break;
				}

				m_GlobalDM->delay(300);
			}
		}
		//FindPic(468, 566, 567, 597, L"choseTeamTaskNext.bmp", &_x, &_y);
		FindPicEx(223, 297, 585, 411, L"taskReady.bmp");

		
		if (_x.lVal > 0) {
			while (true)
			{
				FindPic(195, 233, 604, 369, L"taskTeamWaitOk.bmp", &_x, &_y);
				OutPutListView(nIndex, L"开始组队");
				m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal);
				m_GlobalDM->LeftClick();
				m_GlobalDM->delay(300);
				FindPic(144, 38, 584, 191, L"taskBossYes.bmp", &_x, &_y);
				if (_x.lVal > 0) {
					//for (int k = 0; k < 30; k++)
					//{
					//	m_GlobalDM->KeyDown(37);
					//	m_GlobalDM->delay(300);
					//	m_GlobalDM->KeyPress(16);
					//	m_GlobalDM->delay(300);
					//	m_GlobalDM->KeyUp(37);
					//	m_GlobalDM->delay(300);
					//	m_GlobalDM->KeyDown(39);
					//	m_GlobalDM->delay(300);
					//	m_GlobalDM->KeyPress(16);
					//	m_GlobalDM->delay(300);
					//	m_GlobalDM->KeyUp(39);
					//	m_GlobalDM->delay(300);
					//}
					break;
				}
			}

		}
		FindPic(144, 38, 584, 191, L"taskBossYes.bmp", &_x, &_y);
		if (_x.lVal > 0) {
			OutPutListView(nIndex, L"进入boss");
			int i = 0;
			while (flag)
			{
				//补血
				//打怪
				//找怪
				bossAttack(nIndex);
				//FindPic(603-20, 533-20, 703+20, 569+20, L"taskBossEnd.bmp", &_x, &_y);
				
				FindPic(320, 12, 508, 108, L"taskBossEnd1.bmp", &_x, &_y);
				
				if (_x.lVal > 0) {
					OutPutListView(nIndex, L"boss结束");
					m_GlobalDM->delay(2000);
					m_GlobalDM->KeyPress(16);
					flag=false;
					break;
				}
				i++;
			}
		}
		

		if (!flag) {
			m_GlobalDM->SetWindowState(m_GGameHwnd, 12);

			m_GlobalDM->delay(500);
			m_GlobalDM->KeyDown(39);
			m_GlobalDM->delay(5000);
			//m_GlobalDM->KeyPress(39);
			m_GlobalDM->delay(500);
			m_GlobalDM->KeyUp(39);
		}

		


	}
	return 0;
	//int i, j, k, l;
	//k = 5;
	//l = 8;
	//while (true)
	//{
	//	i = 0, j = 0;
	//	while (true) {
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyDown(39);
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyPress(39);
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyUp(39);
	//		m_GlobalDM->delay(100);
	//		m_GlobalDM->KeyPress(17);
	//		i++;
	//		if (i > k) {
	//			l = 5; k = 5;
	//			break;
	//		}
	//	}

	//	while (true) 
	//	{
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyDown(37);
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyPress(37);
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyUp(37);
	//		m_GlobalDM->delay(100);
	//		m_GlobalDM->KeyPress(17);
	//		j++;
	//		if (j > l) {
	//			k =8;
	//			l = 5;
	//			break;
	//		}
	//	}
	//}
	//return 0;
	//VARIANT _x, _y;
	CString sTaskName=L"";

	m_GlobalDM->SetWindowState(m_GGameHwnd, 12);

	while (m_GState) {

	}
	//while (m_GState)
	//{
	//	m_GlobalDM->MoveToEx(100, 100, 50, 50);
	//	if (sTaskName == L"") {//关闭广告页面
	//		m_GlobalDM->FindPic(732, 56, 779, 88, L"closeWnd2.bmp", L"303030", m_fSim, 0, &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
	//			m_GlobalDM->LeftClick();
	//		}
	//		m_GlobalDM->delay(500);
	//		//sTaskName = L"任务开始";
	//	}
	//	else if (sTaskName == L"任务开始") {
	//		m_GlobalDM->MoveToEx(100, 100, 50, 50);
	//		m_GlobalDM->FindPic(0, 155, 153, 217, L"taskRun.bmp", L"303030", m_fSim, 0, &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1500);
	//		}
	//		m_GlobalDM->FindPic(224, 196, 662, 400, L"taskDong.bmp", L"303030", m_fSim, 0, &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1500);
	//		}
	//		m_GlobalDM->FindPic(226 - 5, 286 - 5, 561 + 5, 478 + 5, L"taskBoos.bmp", L"303030", m_fSim, 0, &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1500);
	//		}
	//		m_GlobalDM->MoveToEx(100, 100, 50, 50);
	//		m_GlobalDM->FindPic(313, 493, 491, 527, L"taskBoosOk.bmp", L"303030", m_fSim, 0, &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, 20, 10);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1500);
	//		}
	//		m_GlobalDM->delay(1500);
	//		FindPic(130, 66, 652, 290, L"bossBack.bmp", &_x, &_y);
	//		if (_x.lVal > 0) {
	//			sTaskName = L"查找npc";
	//		}
	//	}
	//	else if (sTaskName == L"查找npc") {
	//		FindPic(443, 245, 795, 317, L"tagPlan.bmp", &_x, &_y);
	//		if (_x.lVal > 0) {
	//			sTaskName = L"飞机定位";
	//		}
	//		m_GlobalDM->SetWindowState(m_GGameHwnd, 12);
	//		m_GlobalDM->delay(500);
	//		m_GlobalDM->KeyDown(39);
	//		m_GlobalDM->delay(1000);
	//		m_GlobalDM->KeyPress(39);
	//		m_GlobalDM->delay(1000);
	//		m_GlobalDM->KeyUp(39);
	//		m_GlobalDM->delay(1000);
	//	}else if (sTaskName == L"飞机定位") {
	//		for (int i = 0; i < 10; i++)
	//		{
	//			FindPic(3, 376, 776, 462, L"taskNpc3.bmp", &_x, &_y);
	//			if (_x.lVal > 0) {
	//				m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal+20);
	//				m_GlobalDM->LeftClick();
	//				m_GlobalDM->delay(2300);
	//				break;
	//			}
	//			m_GlobalDM->delay(300);
	//		}
	//		FindPic(65, 499, 364, 535, L"choseTeamTask.bmp", &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal + 5);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1300);
	//		}
	//		FindPic(468, 566, 567, 597, L"choseTeamTaskNext.bmp", &_x, &_y);
	//		if (_x.lVal > 0) {
	//			m_GlobalDM->MoveTo(_x.lVal + 10, _y.lVal);
	//			m_GlobalDM->LeftClick();
	//			m_GlobalDM->delay(1300);
	//			//TODO:查找任务是否已完成 完成标记m_GState为假  taskBossEnd.bmp

	//			sTaskName = L"等待组队";
	//			
	//		}
	//	}else if (sTaskName == L"等待组队") {
	//		FindPicEx(234, 330, 572, 389, L"taskReady.bmp");
	//		//TODO:等待连接进入后跳出
	//		while (true)
	//		{
	//			FindPicEx(300, 255, 390, 399, L"taskReady.bmp");

	//		}
	//		//TODO：进入BOSS
	//		//TODO:按键移动并 查找背景是否一直存在如果一直存在则换方向
	//		//TODO:查找结束标志
	//		//TODO:查找光线 光线的位置-任务的位置进行移动

	//	}
	//	m_GlobalDM->delay(2500);
	//}

	//VARIANT _x, _y;
	//m_DM[iDmIndex]->UseDict(1);

	//m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"开始主线";
	//ShowConsoleInfo(iDmIndex, 9);

	//CString strOutPut;
	//m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;

	//while (true)
	//{

	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num++;
	//	strOutPut.Format(L"[主线]计次[%d]", m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strOutPut;
	//	ShowConsoleInfo(iDmIndex, 9);
	//	//ClearUI(iDmIndex,"主线-");
	//	//CloseUI(iDmIndex);
	//	Sleep(2000);
	//}
	return 0;
}

#pragma region ClearUI
void ClearUI(int iDmIndex, CString strTip)
{
	//VARIANT _x, _y;
	//m_DM[iDmIndex]->UseDict(1);

	//m_DM[iDmIndex]->FindStr(754, 549, 784, 579, L"对话", L"a79171-151515", 0.8, &_x, &_y);//右边
	//if (_x.lVal >= 0 && _y.lVal >= 0)
	//{
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strTip + L"对话";
	//	ShowConsoleInfo(iDmIndex, 9);

	//	MoveClick(iDmIndex, 660, 471);
	//	Sleep(1500);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	//}
	//m_DM[iDmIndex]->FindStr(42, 540, 76, 581, L"对话", L"a79171-151515", 0.8, &_x, &_y);//左边
	//if (_x.lVal >= 0 && _y.lVal >= 0)
	//{
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strTip + L"对话";
	//	ShowConsoleInfo(iDmIndex, 9);

	//	MoveClick(iDmIndex, 660, 471);
	//	Sleep(1500);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	//}
	//m_DM[iDmIndex]->FindStr(114, 3, 146, 27, L"回合", L"D9BE97-263233", 0.8, &_x, &_y);//
	//if (_x.lVal >= 0 && _y.lVal >= 0)
	//{
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strTip + L"回合";
	//	ShowConsoleInfo(iDmIndex, 9);

	//	MoveClick(iDmIndex, 615, 553);
	//	Sleep(1500);
	//	MoveClick(iDmIndex, 530, 257);
	//	Sleep(1500);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	//}
	//m_DM[iDmIndex]->FindStr(697, 11, 727, 42, L"跳", L"BAAA91-2A2727", 0.8, &_x, &_y);//
	//if (_x.lVal >= 0 && _y.lVal >= 0)
	//{
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strTip + L"跳过剧情";
	//	ShowConsoleInfo(iDmIndex, 9);
	//	MoveClick(iDmIndex, _x.lVal, _y.lVal);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	//}
	//m_DM[iDmIndex]->FindStr(713, 398, 733, 419, L"使_用", L"D0C0B0-282828", 0.8, &_x, &_y);//
	//if (_x.lVal >= 0 && _y.lVal >= 0)
	//{
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = strTip + L"使_用";
	//	ShowConsoleInfo(iDmIndex, 9);
	//	MoveClick(iDmIndex, _x.lVal, _y.lVal);
	//	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	//}

}
void CloseUI(int iDmIndex)
{

	VARIANT _x, _y;
	m_DM[iDmIndex]->UseDict(1);

	m_DM[iDmIndex]->FindStr(751, 7, 784, 35, L"关闭", L"D4C4B1-202730", 0.8, &_x, &_y);//右边
	if (_x.lVal >= 0 && _y.lVal >= 0)
	{
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"[关闭]关闭";
		ShowConsoleInfo(iDmIndex, 9);

		MoveClick(iDmIndex, 767, 21);
		Sleep(1500);
		m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;
	}

}
#pragma endregion





//////////////////////////////////////////日常//////////////////////////////////







//////////////////////////////////////////////////////////////////////////////////////////
//换角色
int ChangeRole(int iDmIndex)
{
	VARIANT _x, _y;
	m_DM[iDmIndex]->UseDict(1);

	m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"换角色中";
	ShowConsoleInfo(iDmIndex, 9);

	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex++;
	ShowConsoleInfo(iDmIndex, 5);
StartHere:

	for (int i = 0; i < 2; i++)
	{
		CloseUI(iDmIndex);
		ClearUI(iDmIndex, L"换角色-");
	}
	MoveClick(iDmIndex, 309, 565);//设置
	Sleep(2000);
	for (int i = 0; i<10; i++)
	{
		m_DM[iDmIndex]->FindStr(129, 339, 151, 362, L"切", L"CBBCAD-2D3841", 0.8, &_x, &_y);
		if (_x.lVal >= 0 && _y.lVal >= 0)
		{
			m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"[换角色]切换角色";
			ShowConsoleInfo(iDmIndex, 9);
			MoveClick(iDmIndex, 138, 347);
			Sleep(2000);
			MoveClick(iDmIndex, 447, 329);//确定
			break;
		}
		Sleep(1000);
	}

	for (int i = 0; i < 10; i++)
	{
		m_DM[iDmIndex]->FindStr(489, 412, 511, 442, L"与", L"e7f7fd-202020", 0.8, &_x, &_y);
		if (_x.lVal >= 0 && _y.lVal >= 0)
		{
			m_ScriptSet[iDmIndex].m_GameInfoSt.m_strTaskState = L"[换角色]与xx好友玩";
			ShowConsoleInfo(iDmIndex, 9);
			m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num = 0;


			CString strRoleInex;
			strRoleInex.Format(L"%d", m_ScriptSet[iDmIndex].m_iControlIndex + 1);
			g_AccountFile.SetInt(strRoleInex, L"角色", m_ScriptSet[iDmIndex].m_GameInfoSt.m_iRoleIndex);

			return 1;

		}
		Sleep(1000);
	}
	m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num++;
	CString strTip;
	strTip.Format(L"[换角色]错误计次%d", m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num);
	ShowConsoleInfo(iDmIndex, 9);

	if (m_ScriptSet[iDmIndex].m_GameInfoSt.m_iOutTime_Num<5)
	{
		goto StartHere;
	}
	return 0;
}


void WanShiTongDeKaoYan(int iDmIndex) {

}
void XueYuanRenWu(int iDmIndex) {

}

void OutPutListView(int nIndex,CString str) {
	g_MobileGameDlg->m_list_info.SetItemText(nIndex, 5, str);

}


void FindPic(long x1, long y1, long x2, long y2, CString str, VARIANT * x, VARIANT * y,long offsetX,long offsetY) {
	m_GlobalDM->FindPic(x1-5, y1-5, x2+5, y2+5, str, L"303030", m_fSim, 0, x, y);
}

void FindPicEx(long x1, long y1, long x2, long y2, CString str, long offsetX, long offsetY) {
	VARIANT _x, _y;
	FindPic(x1, y1, x2, y2, str, &_x, &_y, offsetX, offsetY);
	if (_x.lVal > 0) {
		m_GlobalDM->MoveToEx(_x.lVal, _y.lVal, offsetX, offsetY);
		m_GlobalDM->LeftClick();
		m_GlobalDM->delay(1000);
	}
}


void bossAttack(int nIndex) {
	int nrightNum = m_GlobalDM->GetColorNum(628, 275, 798, 303, L"fbe9a2", 0.9);
	int nleftNum = m_GlobalDM->GetColorNum(0, 275, 170, 303, L"fbe9a2", 0.9);

	if (nrightNum <= 10 && nleftNum <= 10) { //boss占满屏幕直接攻击
		OutPutListView(nIndex, L"boss占满屏幕");
		for (int i = 0; i < 10; i++)
		{
			addBlood();
			m_GlobalDM->KeyPress(16);
			m_GlobalDM->delay(300);
		}
		
	}
	for (int i = 0; i < 10; i++) //行走左边7次每次查找boss是否存在
	{
		addBlood();
		nleftNum = m_GlobalDM->GetColorNum(0, 275, 170, 303, L"fbe9a2", 0.9);
		if (nleftNum <= 5) {//找到boss攻击
			OutPutListView(nIndex, L"找到左边位置");
			for (int i = 0; i < 10; i++)
			{
				addBlood();
				m_GlobalDM->KeyPress(16);//按shift
				m_GlobalDM->delay(300);
				//nleftNum = m_GlobalDM->GetColorNum(0, 275, 170, 303, L"fbe9a2", 0.9);
				//if (nleftNum <= 5) {
				//	i = 5;
				//}
			}
		}
		else {//未找到移动
			m_GlobalDM->KeyDown(37);
			m_GlobalDM->delay(2000);
			m_GlobalDM->KeyUp(37);
			m_GlobalDM->delay(200);
		}
		m_GlobalDM->delay(500);
		nrightNum = m_GlobalDM->GetColorNum(628, 275, 798, 303, L"fbe9a2", 0.9);
		if (nrightNum <= 0) {
			break;
		}
	}
	for (int i = 0; i < 10; i++) //行走右7次每次查找boss是否存在
	{
		addBlood();
		nrightNum = m_GlobalDM->GetColorNum(628, 275, 798, 303, L"fbe9a2", 0.9);
		if (nrightNum <= 5) {//找到boss攻击
			OutPutListView(nIndex, L"找到右边位置");
			for (int i = 0; i < 10; i++)
			{
				addBlood();
				m_GlobalDM->KeyPress(16);
				m_GlobalDM->delay(300);
				//nrightNum = m_GlobalDM->GetColorNum(628, 275, 798, 303, L"fbe9a2", 0.9);
				//if (nrightNum <= 5) {
				//	i = 5;
				//}
			}
		}
		else {//未找到移动
			m_GlobalDM->KeyDown(39);
			m_GlobalDM->delay(2000);
			m_GlobalDM->KeyUp(39);
			m_GlobalDM->delay(200);
		}
		m_GlobalDM->delay(500);
		nleftNum = m_GlobalDM->GetColorNum(0, 275, 170, 303, L"fbe9a2", 0.9);
		if (nleftNum <= 0) {
			break;
		}
	}
	m_GlobalDM->delay(500);
}
//补血
void addBlood() {
	if (m_GlobalDM->CmpColor(549, 549, L"fb6bd1", 0.9) == 1) {
		m_GlobalDM->KeyPress(35);
	}
	
}